package asia.doing.service.user;

import asia.doing.common.utils.TokenUtils;
import asia.doing.common.utils.UUIDUtils;
import asia.doing.entity.Account;
import asia.doing.entity.Likes;
import asia.doing.mapper.LikesMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service(value = "frontLikesService")
public class FrontLikesService {
    @Resource
    private LikesMapper likesMapper;

    /**
     * 点赞或取消点赞
     * @param likes
     */
    public void set(Likes likes){
        Account currentUser = TokenUtils.getCurrentUser();
        likes.setUser_id(currentUser.getId());
        Likes dblLikes = likesMapper.selectUserLikes(likes);
        System.out.println("喜欢的次数是多少次" + likes.getFid()+likes.getId()+likes.getUser_id()+likes.getModule());
        System.out.println();
        if(dblLikes == null){
            // 使用uuid作为唯一性的id进行插入数据
            likes.setId(UUIDUtils.getUUIDInOrderId());
            System.out.println("我获取到的id是" + likes.getId());
            likesMapper.insert(likes);
        }else{
            System.out.println("要删除的id是" + dblLikes.getId());
            likesMapper.deleteById(dblLikes.getId());
        }
    }

    /**
     * 查询当前用户是否点过赞
     * 点过赞的时候就会返回null不是点赞之后不能返回null
     */
    public Likes selectUserLikes(Long fid, String modules){
        Account currentUser = TokenUtils.getCurrentUser();
        Likes likes = new Likes();
        likes.setUser_id(currentUser.getId());
        likes.setFid(fid);
        likes.setModule(modules);
        return likesMapper.selectUserLikes(likes);
    }


    public int selectByFidAndModule(Long fid, String module){
        return likesMapper.selectByFidAndModule(fid,module);
    }
}
